package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @ClassName ProductUnitController
 * @Author 波
 * @Date 2024/7/28 14:43
 */
@RestController
@Tag(name = "商品单位接口管理")
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private ProductUnitService productUnitService;

    @GetMapping("/page")
    @Operation(summary = "获取商品单位分页列表")
    public TableDataInfo page(Integer pageNum, Integer pageSize, ProductUnit productUnit) {
        Page<ProductUnit> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ProductUnit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(productUnit.getName()), ProductUnit::getName, productUnit.getName());
        page = productUnitService.page(page, queryWrapper);
        return getDataTable(page);
    }
    @GetMapping("/{id}")
    @Operation(summary = "获取商品单位详细信息")
    public AjaxResult getProductUnitDetailInfo(@PathVariable Integer id) {
        return success(productUnitService.getById(id));
    }
    @PostMapping
    @Operation(summary = "新增商品单位")
    public AjaxResult addProductUnit(@RequestBody ProductUnit productUnit) {
        productUnit.setCreateBy(SecurityUtils.getUsername());
        return success(productUnitService.save(productUnit));
    }
    @Operation(summary = "修改商品单位")
    @PutMapping
    public AjaxResult updateProductUnit(@RequestBody ProductUnit productUnit) {
        productUnit.setUpdateBy(SecurityUtils.getUsername());
        return success(productUnitService.updateById(productUnit));
    }
    @Operation(summary = "删除商品单位")
    @DeleteMapping("/{ids}")
    public AjaxResult deleteProductUnit(@PathVariable Long[] ids) {
        return toAjax(productUnitService.removeBatchByIds(Arrays.asList(ids)));
    }
    @Operation(summary = "查询所有商品单位")
    @GetMapping
    public AjaxResult getAllProductUnitList() {
        List<ProductUnit> list = productUnitService.list();
        return success(list);
    }
}
